package com.wendy.common.dao.mybatis.handler;

import com.wendy.common.core.constant.BaseEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.lang.reflect.ParameterizedType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: ALEX WANG
 * @Date: 2020/3/24 9:43
 */
@MappedTypes(BaseEnum.class)
public class BaseEnumTypeHandler<T extends BaseEnum ,C> extends BaseTypeHandler<BaseEnum<T ,C>> {
    private final  Class<T> clazz;
    public BaseEnumTypeHandler(Class<T> clazz) {
        this.clazz=clazz;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, BaseEnum<T, C> parameter, JdbcType jdbcType) throws SQLException {
        if (jdbcType == null) {
            ps.setObject(i, parameter.getCode());
        } else {
            ps.setObject(i, parameter.getCode(), jdbcType.TYPE_CODE);
        }
    }

    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
        ParameterizedType parameterizedType = (ParameterizedType)clazz.getGenericInterfaces()[0];
        String actualTypeName = parameterizedType.getActualTypeArguments()[1].getTypeName();
        if(StringUtils.equals(actualTypeName,Integer.class.getName())){
            return BaseEnum.getEnumByCode(clazz,rs.getInt(columnName));
        }else if(StringUtils.equals(actualTypeName,Long.class.getName())) {
            return BaseEnum.getEnumByCode(clazz,rs.getLong(columnName));
        }else if(StringUtils.equals(actualTypeName, StringUtils.class.getName())) {
            return BaseEnum.getEnumByCode(clazz,rs.getString(columnName));
        }
        return null;
    }

    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        ParameterizedType parameterizedType = (ParameterizedType)clazz.getGenericInterfaces()[0];
        String actualTypeName = parameterizedType.getActualTypeArguments()[1].getTypeName();
        if(StringUtils.equals(actualTypeName,Integer.class.getName())){
            return BaseEnum.getEnumByCode(clazz,rs.getInt(columnIndex));
        }else if(StringUtils.equals(actualTypeName,Long.class.getName())) {
            return BaseEnum.getEnumByCode(clazz,rs.getLong(columnIndex));
        }else if(StringUtils.equals(actualTypeName, StringUtils.class.getName())) {
            return BaseEnum.getEnumByCode(clazz,rs.getString(columnIndex));
        }
        return null;
    }

    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        ParameterizedType parameterizedType = (ParameterizedType)clazz.getGenericInterfaces()[0];
        String actualTypeName = parameterizedType.getActualTypeArguments()[1].getTypeName();
        if(StringUtils.equals(actualTypeName,Integer.class.getName())){
            return BaseEnum.getEnumByCode(clazz,cs.getInt(columnIndex));
        }else if(StringUtils.equals(actualTypeName,Long.class.getName())) {
            return BaseEnum.getEnumByCode(clazz,cs.getLong(columnIndex));
        }else if(StringUtils.equals(actualTypeName, StringUtils.class.getName())) {
            return BaseEnum.getEnumByCode(clazz,cs.getString(columnIndex));
        }
        return null;
    }
}
